package com.richpencil.utils;

import com.jfinal.kit.HashKit;

import java.math.BigDecimal;
import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.*;

public class MathHelper {

	 
		/** 
		 * SHA1 安全加密算法 
		 * @param maps 参数key-value map集合 
		 * @return 
		 * @throws DigestException
		 */  
	public static String Math_SHA11(Map<String, String> maps) throws DigestException
	{  
		//获取信息摘要 - 参数字典排序后字符串  
		String decrypt = getOrderByLexicographic(maps);
		try {  
			//指定sha1算法  
			MessageDigest digest = MessageDigest.getInstance("SHA-1");
			digest.update(decrypt.getBytes());  
			//获取字节数组  
			byte messageDigest[] = digest.digest();  
			// Create Hex String  
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数  
			for (int i = 0; i < messageDigest.length; i++) {  
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {  
					hexString.append(0);  
				}  
				hexString.append(shaHex);  
			}  
			return hexString.toString().toUpperCase();  
  
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();  
			throw new DigestException("签名错误！");
		}  
    }
	
	public static String Math_SHA1(Map<String, String> map)
	{
		String value="";
		String value1="";
		int i=0;
		for (Map.Entry<String, String> entry : map.entrySet()) {
	       
			System.out.println(entry.getKey()+"\r\n");
			
			if(i==0)
			{
				value=entry.getKey()+"="+entry.getValue();
			}
			else{
				
				value=value+"&"+entry.getKey()+"="+entry.getValue();
			}
			i++;
	    }  
		//System.out.println("write"+value);
		String signature = HashKit.sha1(value);
		return signature;
	}
	private static String getOrderByLexicographic(Map<String, String> maps){
		System.out.println("write1"+splitParams(lexicographicOrder(getParamsName(maps)),maps));
        return splitParams(lexicographicOrder(getParamsName(maps)),maps);  
    }
	private static String splitParams(List<String> paramNames, Map<String, String> maps){
		StringBuilder paramStr = new StringBuilder();
		for(String paramName : paramNames){
			paramStr.append(paramName);  
			for(Map.Entry<String, String> entry : maps.entrySet()){
				if(paramName.equals(entry.getKey())){  
					paramStr.append("="+ String.valueOf(entry.getValue())+"&");
				}  
			}  
		}  
		paramStr.deleteCharAt(paramStr.length()-1);
		return paramStr.toString();  
	}	
	private static List<String> getParamsName(Map<String, String> maps){
		
        List<String> paramNames = new ArrayList<String>();
        for(Map.Entry<String, String> entry : maps.entrySet()){
            paramNames.add(entry.getKey());  
        }  
        return paramNames;  
	}
    private static List<String> lexicographicOrder(List<String> paramNames){
		Collections.sort(paramNames);
		return paramNames;  
	}
    private static double getRandomDouble(int max)
    {
    	
      int min=1;
      java.util.Random random = new java.util.Random();
      double s = random.nextDouble()%(max-min+1) + min;
      return s;
    }
    private static double getRandomInt(int max)
    {

      int min=1;
      java.util.Random random = new java.util.Random();
      double s = random.nextInt(max)*10%(max-min+1) + min;
      return s;
    }
    public static double nextDouble(double max)
    {
    	final double min=1d;
        double result= min + ((max - min) * new Random().nextDouble());
        DecimalFormat df   = new DecimalFormat("######0.00");
        String Strresult=new java.text.DecimalFormat("#.0").format(result);
        result= Double.parseDouble(Strresult);
        return result;
    }
    
    public static BigDecimal getDecimalValue(double max)
    {
		BigDecimal decimal = new BigDecimal(max);
		BigDecimal decimalr=decimal.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		return decimalr;
    }
    
    
    public static BigDecimal getRandomDecimal(double max)
    {
    	
		double temp=MathHelper.nextDouble(max);
		BigDecimal decimal = new BigDecimal(temp);
		BigDecimal decimalr=decimal.setScale(2, BigDecimal.ROUND_HALF_DOWN);
		return decimalr;
    }
    
    public static void main(String[] args) throws Exception {


    	double i=nextDouble(15d);
    	System.out.println(i);
    }
  
}
